/* 
 * File:   widget_histogram.cpp
 * Author: dima
 * 
 * Created on 10 января 2015 г., 15:35
 */

#include "widget_histogram.h"
#include <qwt_plot_grid.h>
#include <qwt_plot_histogram.h>

widget_histogram::widget_histogram(QWidget *widget) : QwtPlot(widget)
{
    init_window();
}

widget_histogram::widget_histogram(const QwtText &title, QWidget *widget) : QwtPlot(title, widget)
{
    init_window();
}

widget_histogram::~widget_histogram()
{
    delete intervals;
    delete grid;
    delete histogram;
}

void widget_histogram::set_data()//Set the data for the histogram
{
    histogram->setSamples(*intervals);
    histogram->attach(this);
    this->replot();

}

void widget_histogram::add_interval(double x_1, double x_2, double y)//Add interval  for the histogram
{
    intervals->append(QwtIntervalSample(y, x_1, x_2));
}

void widget_histogram::init_window()//initialization window
{
    setCanvasBackground(Qt::white); // background color
    // Enable grid
    grid = new QwtPlotGrid();
    grid->setMajorPen(QPen(Qt::gray, 2)); // line color and thickness
    grid->attach(this); 

    // Create a histogram
    histogram = new QwtPlotHistogram();
    histogram->setStyle(QwtPlotHistogram::Columns);

    histogram->setPen(QPen(QColor(50, 60, 200)));
    histogram->setBrush(QBrush(QColor(50, 100, 220)));

    intervals = new QVector<QwtIntervalSample>;
}